package per.zhang.serverPrevioder.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import per.zhang.serverPrevioder.base.utils.KeyUtil;
import per.zhang.serverPrevioder.base.utils.ResultUtil;
import per.zhang.serverPrevioder.base.dto.ResultDTO;
import per.zhang.serverPrevioder.base.page.PageList;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import per.zhang.serverPrevioder.dto.request.PageDTO;
import per.zhang.serverPrevioder.dto.respone.VaccineOrderRespondDTO;
import per.zhang.serverPrevioder.dto.respone.VaccineRespondDTO;
import per.zhang.serverPrevioder.entity.Vaccine;
import per.zhang.serverPrevioder.service.VaccineOrderService;
import per.zhang.serverPrevioder.entity.VaccineOrder;
import per.zhang.serverPrevioder.service.VaccineService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 *
 * @author Zhang
 * @since 2021-01-25
 */
@RestController
@RequestMapping("/vaccineOrder")
@CrossOrigin
@Slf4j
public class VaccineOrderController {

        @Autowired
        private VaccineOrderService vaccineOrderService;

        @Autowired
        private VaccineService vaccineService;

        @PostMapping("/insertVaccineOrder")
        @ApiOperation(value = "疫苗入库")
        //@ApiImplicitParams()
        public ResultDTO insertOne( @RequestBody VaccineOrder vaccineOrder)throws Exception{
            try {
                vaccineOrder.setTotalPrice(vaccineOrder.getVaccinePrice().multiply(new BigDecimal(vaccineOrder.getVaccineNum())));
                vaccineOrder.setCreatTime(new Date());
                vaccineOrder.setOrderId(KeyUtil.getUniqueKey());
                vaccineOrderService.save(vaccineOrder);
                Vaccine vaccine = vaccineService.get(vaccineOrder.getVaccineId());
                vaccine.setSurplusNum(vaccineOrder.getVaccineNum()+vaccine.getSurplusNum());
                vaccineService.update(vaccine);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(vaccineOrder);
        }

        @PostMapping("/getAllPage")
        @ApiOperation("获取订单")
        public ResultDTO getAllPage(@RequestBody PageDTO pageDTO)throws Exception{
            PageList<VaccineOrderRespondDTO> result = null;
            try {
                PageList<VaccineOrder> pageData = vaccineOrderService.findPageData(new VaccineOrder(), pageDTO.getPageNum(), pageDTO.getPageSize(),"creat_time DESC");
                ArrayList<VaccineOrderRespondDTO> data = new ArrayList<>();
                for (VaccineOrder item : pageData.getData()) {
                    Vaccine vaccine = vaccineService.get(item.getVaccineId());
                    data.add(new VaccineOrderRespondDTO(item, vaccine.getVaccineName()));
                }
                result = new PageList<>(data,pageData.getPageNum(),pageData.getPageSize(),pageData.getTotalItems());
            }catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(result);
        }

        @PostMapping("/updateById")
        @ApiOperation(value = "按id修改数据")
        //@ApiImplicitParams()
        public ResultDTO updateById(  VaccineOrder vaccineOrder)throws Exception{
            try {
                vaccineOrderService.update(vaccineOrder);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(vaccineOrderService.get(vaccineOrder.getVaccineId()));

        }

        @GetMapping("/deleteById/{id}")
        @ApiOperation("按id删除数据")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name = "id", dataType = "string", required = true, value = "实体类id"),

        })
        public ResultDTO deleteById(@RequestParam(required = true) @PathVariable("id") String id)throws Exception{
            VaccineOrder oldVaccineOrder = null;
            try {
                oldVaccineOrder = vaccineOrderService.get(id);
                vaccineOrderService.deleteById(id);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(oldVaccineOrder);
        }

        @PostMapping("/getVaccineOrderPage")
        @ApiOperation("按条件查询，分页")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "页码"),
                @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页容量")
        })
        public ResultDTO selectEntityPage(  VaccineOrder vaccineOrder,
                                              int pageNum,
                                              int pageSize)throws Exception{

            PageList<VaccineOrder> pageData = null;
            try {
                pageData = vaccineOrderService.findPageData(vaccineOrder, pageNum, pageSize,"creat_time DESC");
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(pageData);

        }

        @PostMapping("/adminGetVaccineOrderPage")
        @ApiOperation("用户获取仓库订单，分页")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "页码"),
                @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页容量")
        })
        public ResultDTO adminGetVaccineOrderPage(@RequestBody PageDTO pageDTO)throws Exception{

            PageList<VaccineOrder> pageData = null;
            try {
                pageData = vaccineOrderService.findPageData(new VaccineOrder(), pageDTO.getPageNum(), pageDTO.getPageSize(),"creat_time DESC");
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(pageData);

        }

        @PostMapping("/getVaccineOrder")
        @ApiOperation("按条件查询，不分页")
        //@ApiImplicitParams()
        public ResultDTO selectEntityPage(  VaccineOrder vaccineOrder)throws Exception{
            List<VaccineOrder> vaccineOrderList = null;
            try {
                vaccineOrderList = vaccineOrderService.findByParams(vaccineOrder,"creat_time DESC");
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(vaccineOrderList);
        }


}
